package com.tank.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.tank.bean.User;
import com.tank.bean.item.Comment;
import com.tank.bean.item.Item;
import com.tank.bean.item.Talk;
import com.tank.bean.item.Transpond;
import com.tank.core.TankModel;
import com.tank.dao.ItemDAO;
import com.tank.dao.UserDAO;
import com.tank.service.ItemService;
import com.tank.util.NoticeUtil;
import com.tank.util.TankUtil;
import com.tank.vo.RsVo;

@Service("itemService")
public class ItemServiceImpl implements ItemService {

	@Autowired
	@Qualifier("itemDao")
	ItemDAO dao;
	
	@Autowired
	@Qualifier("userDao")
	UserDAO userDao;
	
	TankModel tm;
	public TankModel favour(Item item) {
		tm=tm.newInstance();
		tm.setMessage("赞！");
		dao.favour(item);
		return tm;
	}

	public RsVo transpond(Item item) {
		RsVo rs = new RsVo();

		if (item == null)
			return rs;

		item.setAuthor(TankUtil.getCurrentUser());
		item.setPubtime(new Date());

		item.setFavourCount(0);
		item.setCommentCount(0);
		item.setTranspondCount(0);
		dao.transpond(item);

		rs.setStatus(true);
		rs.setMessage("成功");
		return null;
	}

	public RsVo comment(Item item) {

		RsVo rs = new RsVo();

		if (item == null)
			return rs;

		item.setAuthor(TankUtil.getCurrentUser());
		item.setPubtime(new Date());

		item.setFavourCount(0);
		item.setCommentCount(0);
		item.setTranspondCount(0);

		dao.comment(item);

		rs.setStatus(true);
		rs.setMessage("成功");

		return rs;
	}

	public RsVo remove(Item item) {
		RsVo rs = new RsVo();
		dao.hide(item);
		rs.setStatus(true);
		rs.setMessage("成功");
		return rs;
	}

	public boolean publish(Item item) {

		if (item == null)
			return false;
		
		StringBuilder names = new StringBuilder();
		
	
		//==提到某人====
		Pattern pat=Pattern.compile("@(\\w{1,20})(\\s|\\b)");
		Matcher mat=pat.matcher(item.getContent());
		System.out.println("item.content='"+item.getContent()+"'");
		while(mat.find()){
			System.out.println(mat.group(1));
			names.append("'"+mat.group(1)+"',");
		}
		
		String con=item.getContent();
		if(names.length()>0){
			names.append("'38'");
			List<User> users=userDao.findUsersByNames(names.toString());
			for(User u:users){
				con=con.replace("@"+u.getName(), "["+u.getId()+","+u.getName()+"]");
			}
			
			item.setContent(con);
			System.out.println(con);
			
		}
		
		
		
		
		//==提到某人====
		
		
		
		item.setAuthor(TankUtil.getCurrentUser());
		item.setPubtime(new Date());

		item.setFavourCount(0);
		item.setCommentCount(0);
		item.setTranspondCount(0);
		
		
		item.setHide(false);

		dao.save(item);
		return true;
	}

	public Talk addTalk(Talk talk) {
		
		talk.setAuthor(TankUtil.getCurrentUser());
		talk.setPubtime(new Date());
		talk.setFavourCount(0);
		talk.setCommentCount(0);
		talk.setTranspondCount(0);
		talk.setHide(false);
		
		
		atUser(talk);
		
		dao.save(talk);
		
		return talk;
	}

	private void atUser(Item item) {
		StringBuilder names = new StringBuilder();
		
		
		//==提到某人====
		Pattern pat=Pattern.compile("@(\\w{1,20})(\\s|\\b)");
		Matcher mat=pat.matcher(item.getContent());
		
		while(mat.find()){
			//System.out.println(mat.group(1));
			names.append("'"+mat.group(1)+"',");
		}
		
		String con=item.getContent();
		if(names.length()>0){
			names.append("'38'");
			List<User> users=userDao.findUsersByNames(names.toString());
			for(User u:users){
				con=con.replace("@"+u.getName(), "["+u.getId()+","+u.getName()+"]");
				item.getAtUsers().add(u);
				//通知
				NoticeUtil.addAt(TankUtil.getCurrentUser(), u);
			}
			
			item.setContent(con);
			//System.out.println(con);
			
		}
		
		
	}

	public Transpond addTranspond(Transpond transpond) {
		transpond.setAuthor(TankUtil.getCurrentUser());
		transpond.setPubtime(new Date());
		transpond.setFavourCount(0);
		transpond.setCommentCount(0);
		transpond.setTranspondCount(0);
		transpond.setHide(false);
		
		Item parent=dao.findItemById(transpond.getParent().getId());
//		parent.setTranspondCount(parent.getTranspondCount()+1);
		transpond.setParent(parent);
	
		atUser(transpond);
		dao.update(parent);
		dao.save(transpond);
		
		return transpond;
	}

	public Comment addComment(Comment comment) {
		comment.setAuthor(TankUtil.getCurrentUser());
		comment.setPubtime(new Date());
		comment.setFavourCount(0);
		comment.setCommentCount(0);
		comment.setTranspondCount(0);
		comment.setHide(false);
		atUser(comment);
		comment.setParent(dao.get(comment.getParent().getId()));
		dao.save(comment);
		return comment;
	}

	public TankModel findComments(int itemId, int pageNo,int pageSize) {
		return dao.findComments(itemId,pageNo,pageSize);
	}

}
